package com.vivo.tool.eclipsestarter;

import java.io.BufferedReader;
import java.io.*;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Toolkit
 * 
 * @author vivo
 * 
 * @version 0.14
 */
public class Tool {

	public static void println(String str) {
		if (Constant.OUTPUT) System.out.println(str);
	}

	public static void assertPrintln(String str) {
		if (Constant.DEBUG) System.out.println(str);
	}

	/**
	 * read a file to a Arraylist,each line a String item.
	 * 
	 * @param strFilepath
	 * @return
	 */
	public static ArrayList readFileToArrayList(String strFilepath) {
		// if not exist return;
		File file = new File(strFilepath);
		if (file == null || !file.exists()) {
			Tool.println("Error! can't find a file");
			return null;
		}
		file = null;

		// read to a arraylist.
		ArrayList<String> alLine = new ArrayList<String>();
		try {
			BufferedReader in = new BufferedReader(new FileReader(Constant.strMySelection));
			String str;
			while ((str = in.readLine()) != null) {
				alLine.add(str);
			}
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return alLine;
	}

	/**
	 * Copy a file.<BR>
	 * Important! Can copy text file only.
	 * 
	 * @param src -
	 *                File name.
	 * @param dest -
	 *                Destination Folder.
	 */
	public static void copyFile(String src, String dest) {
		// TODO Optimize File copy for speed.

		// if src file not found return.
		File srcFile = new File(src);
		if (srcFile == null || !srcFile.exists()) {
			Tool.println("Error! file not exist");
			return;
		}
		// if dest folder not exists, create it.
		File destDir = new File(dest);
		if (!destDir.exists()) destDir.mkdir();

		// create dest File.
		if (!(dest.charAt(dest.length() - 1) == '\\')) dest += "\\";
		File destFile = new File(dest + srcFile.getName());

		// copy
		try {
			BufferedReader in = new BufferedReader(new FileReader(srcFile));
			BufferedWriter out = new BufferedWriter(new FileWriter(destFile));
			String str;
			while ((str = in.readLine()) != null) {
				out.write(str + "\r\n");
			}
			in.close();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	
	public static void printArrayList(ArrayList al) {
		Tool.println("Print a list " + al.size());
		for (Object obj : al) {
			Tool.println((String) obj);
		}
	}

}
